package com.zll.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zll.domain.PurchaseApply;
import com.zll.domain.Stock;
import com.zll.domain.StockLog;
import com.zll.dto.StockDto;
import com.zll.enums.GlobalEnumException;
import com.zll.exception.GlobalCustomException;
import com.zll.feign.PurchaseService;
import com.zll.mapper.StockLogMapper;
import com.zll.mapper.StockMapper;
import com.zll.properties.StockProperties;
import com.zll.service.IStockService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoyao
 * @since 2022-09-29
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private StockLogMapper stockLogMapper;

    @Autowired
    private StockProperties stockProperties;

    @Autowired
    private PurchaseService purchaseService;


    // 出库
    @Override
    public void out(StockDto stockDto) {
        Integer count = stockDto.getCount();
        Long productId = stockDto.getProductId();
        // 根据商品id查询商品
        Stock stock = stockMapper.selectById(productId);
        // 具体商品的库存
        Integer count1 = stock.getCount();
        // 判断库存不足的时候
        if (count1<count) {
            // 记录日志
            StockLog stockLog = new StockLog();
            stockLog.setTypeId(productId);
            stockLog.setState(0);
            stockLog.setStockTime(new Date());
            stockLog.setProductName(stock.getProductName());
            stockLog.setProductId(stock.getProductId());
            stockLog.setCount(count1);
            stockLog.setLog(stockProperties.getOutwarn());
            stockLogMapper.insert(stockLog);
        }


        // 库存足够分为两种情况
        // 1.库存足够但是小于阈值
        if (count1<stockProperties.getThreshold()) {
            // 记录日志
            StockLog stockLog1 = new StockLog();
            stockLog1.setStockTime(new Date());
            stockLog1.setTypeId(stock.getTypeId());
            stockLog1.setState(-1);
            stockLog1.setProductName(stock.getProductName());
            stockLog1.setProductId(stock.getProductId());
            stockLog1.setLog(stockProperties.getOutwarn());
            stockLog1.setCount(count1);
            stockLogMapper.insert(stockLog1);
            // 库存不足，申请采购
            stock.setStockTime(new Date());
            stock.setCount(stockProperties.getPurchase());
            stock.setProductId(stock.getId());
            // 调用feign接口申请采购
            PurchaseApply purchaseApply = new PurchaseApply();
            BeanUtils.copyProperties(stock,purchaseApply);
            purchaseApply.setCount(100);
            //purchaseApply.setProductName(stock.getProductName());
            //purchaseApply.setProductId(stock.getProductId());
            purchaseApply.setState(0);
            purchaseApply.setPurchaseTime(new Date());
            purchaseService.saveOrUpdate(purchaseApply);

        }
        // 当库存大于阈值时
        StockLog log = new StockLog();
        count1 = count1-count;
        stock.setCount(count1);
        log.setCount(count1);
        stockMapper.updateById(stock);
        stockLogMapper.updateById(log);
        // 记录日志
        StockLog stockLog1 = new StockLog();
        stockLog1.setStockTime(new Date());
        stockLog1.setTypeId(stock.getTypeId());
        stockLog1.setState(0);
        stockLog1.setProductName(stock.getProductName());
        stockLog1.setProductId(stock.getProductId());
        stockLog1.setLog(stockProperties.getOutsuccess());
        stockLog1.setCount(count1);
        stockLogMapper.insert(stockLog1);


    }

    // 入库
    @Override
    public void in(StockDto stockDto) {
        Integer count = stockDto.getCount();
        Long productId = stockDto.getProductId();
        // 根据商品id查询商品
        Stock stock = stockMapper.selectById(productId);
        if (stock==null) {
            throw new GlobalCustomException(GlobalEnumException.product_NO_ERROR);
        }
        // 不为空就获取商品数量
        Integer count1 = stock.getCount();
        //添加库存
        count1 = count1 + count;
        stock.setCount(count1);
        stockMapper.updateById(stock);
        // 记录日志
        StockLog log = new StockLog();
        log.setCount(count1);
        log.setLog(stockProperties.getInsuccess());
        log.setState(1);
        log.setProductName(stock.getProductName());
        log.setProductId(stock.getProductId());
        log.setTypeId(stock.getTypeId());
        log.setStockTime(new Date());
        stockLogMapper.insert(log);

    }

    @Override
    public List<Stock> findOut() {
        // 获取库存阈值
        Integer threshold = stockProperties.getThreshold();
        Wrapper<Stock> wrapper= new EntityWrapper<>();
        wrapper.lt("count",threshold);
        List<Stock> stocks = selectList(wrapper);
        return stocks;

    }


}
